using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.BaseFunctoids;
using System.Reflection;
using System.Collections;

namespace eliasen.eu.BizTalk.Functoids
{
    class CummulativeComma : BaseFunctoid
    {
        private ArrayList myCumulativeArray = new ArrayList();

        public CummulativeComma()
            : base()
        {
            //ID for this functoid
            this.ID = 6432;

            // resource assembly must be ProjectName.ResourceName if building with VS.Net
            SetupResourceAssembly("eliasen.eu.BizTalk.Functoids.eliasen.eu.BizTalk.Functoids.Resources", Assembly.GetExecutingAssembly());

            //Setup the Name, ToolTip, Help Description, and the Bitmap for this functoid
            SetName("Str_CummulativeComma_Name");
            SetTooltip("Str_CummulativeComma_Tooltip");
            SetDescription("Str_CummulativeComma_Description");
            SetBitmap("Bmp_CummulativeComma_Icon");

            this.SetMinParams(1);
            this.SetMaxParams(1);

            //set the function name that needs to be called when this Functoid is invoked. This means that
            //this Functoid assembly need to be present in GAC for its availability during Test..Map and Runtime.
            SetExternalFunctionName(GetType().Assembly.FullName, "eliasen.eu.BizTalk.Functoids.CummulativeComma", "InitializeValue");
            SetExternalFunctionName2("AddValue");
            SetExternalFunctionName3("RetrieveFinalValue");

            //category for this functoid. This functoid goes under the String Functoid Tab in the
            //VS.Net toolbox for functoids.
            this.Category = FunctoidCategory.Cumulative;
            this.OutputConnectionType = ConnectionType.AllExceptRecord;

            AddInputConnectionType(ConnectionType.AllExceptRecord); //first input
        }

        /// <summary>
        /// This is the function that gets called when the Map initializes an instance of the functoid.
        /// </summary>
        /// <param name="index">Index to identify the instane of accumulation</param>
        /// <returns></returns>
        public string InitializeValue(int index)
        {
            //WriteEvent("InitializeValue called with " + index.ToString());
            if (index >= 0)
            {
                if (index >= myCumulativeArray.Count)
                {
                    myCumulativeArray.Add(index);
                    myCumulativeArray[index] = "";
                }
                else
                {
                    myCumulativeArray[index] = "";
                }
            }

            return "";
        }

        private void WriteEvent(string p)
        {
            System.Diagnostics.EventLog.WriteEntry("eliasen", p);
        }

        /// <summary>
        /// This method gets called for all inputs to the funtoid, so the functoid can accumulae the final value.
        /// </summary>
        /// <param name="index">Index to identify the instane of accumulation</param>
        /// <param name="value">The value to add</param>
        /// <param name="scope">A scoping parameter</param>
        /// <returns></returns>
        public string AddValue(int index, string value, string scope)
        {
            //WriteEvent("AddValue called with " + index.ToString() + "   " + value);
            string str = myCumulativeArray[index].ToString();
            str += value + ",";
            myCumulativeArray[index] = str;
            return "";
        }

        public string RetrieveFinalValue(int index)
        {
            //WriteEvent("RetrieveValue called with " + index.ToString());
            string str = myCumulativeArray[index].ToString();
            if (str.Length > 0)
                return str.Substring(0, str.Length - 1);
            else
                return "";
        }
    }
}
